package com.huangguolin.cloud.spring.control;


import com.alibaba.fastjson.JSON;
import com.huangguolin.cloud.mybatis.entity.PtUserEntity;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class BaseController {

    @Resource
    private MessageSource messageSource;

    public String successJSON(String i18bKey,Object ... args){
        Map<String,Object> message = new HashMap<>();
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr = messageSource.getMessage(i18bKey, args, locale);
        message.put("success", true);
        message.put("msg", msgStr);
        message.put("description", msgStr);
        String rst = JSON.toJSONString(message);
        return rst;


    }

    public String successJSONES(String i18bKey, List<PtUserEntity> list , Object ... args){
        Map<String,Object> message = new HashMap<>();
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr = messageSource.getMessage(i18bKey, args, locale);
        message.put("success", true);
        message.put("msg", msgStr);
        message.put("list", list);
        message.put("description", msgStr);
        String rst = JSON.toJSONString(message);
        return rst;


    }

    public String successJSONS(String i18bKey,String code,Object ... args){
        Map<String,Object> message = new HashMap<>();
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr = messageSource.getMessage(i18bKey, args, locale);
        message.put("success", true);
        message.put("msg", msgStr);
        message.put("code", code);
        message.put("description", msgStr);
        String rst = JSON.toJSONString(message);
        return rst;


    }
    public String failJSON(String i18bKey,Object ... args){
        Map<String,Object> message = new HashMap<>();
        Locale locale = LocaleContextHolder.getLocale();
        String msgStr = messageSource.getMessage(i18bKey, args, locale);
        message.put("success", false);
        message.put("msg", msgStr);
        message.put("description", msgStr);
        String rst = JSON.toJSONString(message);
        return rst;


    }

    public String totalJSON(Integer total,Object ... args){
        Map<String,Object> message = new HashMap<>();
        message.put("success", true);
        message.put("total", total);
        String rst = JSON.toJSONString(message);
        return rst;
    }
    public String codeJSON(String code,Object ... args){
        Map<String,Object> message = new HashMap<>();
        message.put("success", true);
        message.put("code", code);
        String rst = JSON.toJSONString(message);
        return rst;
    }


}
